package com.xjl.controller;

import com.xjl.domain.ResponseResult;
import com.xjl.domain.User;
import com.xjl.resolver.CurrentUserId;
import com.xjl.service.UserService;
import com.xjl.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
//@CrossOrigin
public class UserController {

    @Autowired
    private UserService  userService;

        @RequestMapping("/insertUser")
        public ResponseResult insertUser(){
            userService.insertUser();
            return new ResponseResult<>(200,"添加成功");
        }


    @RequestMapping("/findAll")
    public ResponseResult findAll(@CurrentUserId String userid) throws Exception {
        //获取请求头中的token
//        String token=request.getHeader("token");
//        if (StringUtils.hasText(token)){
//            //解析token获取用户id
//            Claims claims= JwtUtil.parseJWT(token);
//            String userid = claims.getSubject();
//            System.out.println(userid);
//        }
        System.out.println(userid);
        List<User> users = userService.findAll();
        return new ResponseResult<>(200,users);
    }
}
